package com.spd.thermometrydemo;

import androidx.appcompat.app.AppCompatActivity;
import temperture.ITempertureInterface;
import temperture.Temperture;
import temperture.TempertureCallBack;
import temperture.TempertureManager;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private ITempertureInterface temperatureManager;
    private TextView tvmubiao;
    private Button btnStart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_read);
        initUI();
        final ProgressDialog pd = new ProgressDialog(this);
        pd.setMessage(getString(R.string.loading));
        pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pd.setCancelable(false);
        pd.show();
        temperatureManager = TempertureManager.getIntance();
        temperatureManager.openDev();
        new Thread(new Runnable() {
            @Override
            public void run() {
                temperatureManager.initData(getApplication());
                pd.dismiss();
            }
        }).start();
        temperatureManager.setCallBack(new TempertureCallBack() {
            @Override
            public void receTempData(final Temperture temperture) {
                String msg = "";
                if (temperture.getBodyTemp() < 29) {
                    msg = getString(R.string.waring_low_temp) + temperture.getBodyTemp();
                } else if (temperture.getBodyTemp() > 39) {
                    msg = getString(R.string.waring_high_temp);
                } else {
                    msg = temperture.getBodyTemp() + "℃";
                }
                if (temperture.getTargetTemp() < 0) {
                    msg = getString(R.string.data_error);
                }
                final String finalMsg = msg;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvmubiao.setText(finalMsg);
                        btnStart.setEnabled(true);
                    }
                });
            }

            @Override
            public void receiveError(int error) {
                if (error == -1) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvmubiao.setText(getString(R.string.url_null));
                            btnStart.setEnabled(true);
                        }
                    });
                }
            }
        });
    }

    private void initUI() {
        tvmubiao = findViewById(R.id.tv_mubiao);
        btnStart = findViewById(R.id.btn_start);
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnStart.setEnabled(false);
                temperatureManager.startMeasurement(true);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        temperatureManager.closeDev();
        super.onDestroy();
    }
}
